package logic

import (
	"context"
	"fmt"
	"shuqi_novel/models/mysql"

	"shuqi_novel/novelrpc/internal/svc"
	"shuqi_novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *novelrpc.LoginRequest) (*novelrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User
	mysql.DB.Debug().Where("account = ?", in.Account).Find(&findUser)
	if findUser.ID == 0 {
		return nil, fmt.Errorf("当前账号未注册，请前去注册")
	}
	if findUser.Password != in.Password {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}

	return &novelrpc.LoginResponse{
		UserId: int64(findUser.ID),
	}, nil
}
